package cn.liumouren.jzxd.task;

import cn.liumouren.jzxd.dao.NewsDao;
import cn.liumouren.jzxd.dao.PictureDao;
import cn.liumouren.jzxd.dao.SceneDao;
import cn.liumouren.jzxd.entity.News;
import cn.liumouren.jzxd.entity.Picture;
import cn.liumouren.jzxd.entity.Scene;
import cn.liumouren.jzxd.utils.mapperUtils;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时从oss删除垃圾图片
 *
 * @auther: Freeman Liu
 * @date: 2020/11/1 12:00
 */
@Component
public class CleanOssPic {
    @Resource
    private OSS ossClient;
    @Resource
    private PictureDao pictureDao;
    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;

    @Resource
    private SceneDao sceneDao;
    @Resource
    private NewsDao newsDao;

    /**
     * 每天3点清理一次图片
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanOssPic() {
        // 找出没有被引用的picture
        List<Picture> pictures = pictureDao.selectList(new QueryWrapper<Picture>().lambda()
                .select(Picture::getId, Picture::getUrl)
                .isNull(Picture::getGoodsId)
                .isNull(Picture::getSceneId)
                .isNull(Picture::getNewsId)
                .isNull(Picture::getZoneId)
                .isNull(Picture::getReportId)
                .and(pictureLambdaQueryWrapper -> pictureLambdaQueryWrapper
                        .isNull(Picture::getSurroundingPic)
                        .or()
                        .eq(Picture::getSurroundingPic, 0)));

        // https://hey-go.oss-cn-chengdu.aliyuncs.com/2020-11-01/7785bc39-b881-46a5-96e8-5bd3018099c087526
        // 获取 keys
        List<String> keys = new ArrayList<>();
        if (pictures.size() > 0) {
            keys = pictures.stream()
                    .map(picture -> picture.getUrl().replace("https://hey-go.oss-cn-chengdu.aliyuncs.com/", ""))
                    .collect(Collectors.toList());
            // 批量删除图片
            if (keys.size() > 0) {
                System.out.println("正在删除oss里的图片......");
                ossClient.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys));
                System.out.println("删除oss里的图片完成!");
            }

            // 删除数据库中的数据
            List<String> ids = pictures.stream().map(Picture::getId).collect(Collectors.toList());
            if (ids.size() > 0) {
                pictureDao.deleteBatchIds(ids);
            }
        }

        System.out.println("本次操作完成!!!");
        System.out.println("本次删除[" + keys.size() + "]张垃圾图片!");
    }

    public void bindPic() {
        List<Scene> scenes = sceneDao.selectList(null);
        for (Scene scene : scenes) {
            mapperUtils.associatePicturesAndEntity(scene, pictureDao);
        }
    }

    public void delNews() {
        int count = newsDao.delete(new UpdateWrapper<News>().lambda().isNull(News::getContent));
        System.out.println("共删除" + count + "条新闻");
    }
}
